package com.neusoft.elm_servlet.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm_servlet.dao.BusinessDao;
import com.neusoft.elm_servlet.po.Business;
import com.neusoft.elm_servlet.util.DBUtil;

public class BusinessDaoImpl implements BusinessDao {

	@Override
	public List<Business> listBusinessByOrderTypeId(String orderTypeId) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("select * from business where orderTypeId=?");
		ps.setString(1, orderTypeId);
		ResultSet rs = ps.executeQuery();
		List<Business> list = new ArrayList<>();
		while(rs.next()){
			Business business = new Business();
			business.setBusinessId(rs.getInt("businessId"));
			business.setBusinessName(rs.getString("businessName"));
			business.setBusinessAddress(rs.getString("businessAddress"));
			business.setBusinessExplain(rs.getString("businessExplain"));
			business.setBusinessImg(rs.getString("businessImg"));
			business.setOrderTypeId(rs.getInt("orderTypeId"));
			business.setStarPrice(rs.getDouble("starPrice"));
			business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
			business.setRemarks(rs.getString("remarks"));
			list.add(business);
		}
		DBUtil.close(rs, ps, con);
		return list;
	}

	@Override
	public Business getBusinessById(String businessId) throws SQLException {
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement("select * from business where businessId=?");
		ps.setString(1, businessId);
		ResultSet rs = ps.executeQuery();
		Business business = new Business();
		while(rs.next()){
			business.setBusinessId(rs.getInt("businessId"));
			business.setBusinessName(rs.getString("businessName"));
			business.setBusinessAddress(rs.getString("businessAddress"));
			business.setBusinessExplain(rs.getString("businessExplain"));
			business.setBusinessImg(rs.getString("businessImg"));
			business.setOrderTypeId(rs.getInt("orderTypeId"));
			business.setStarPrice(rs.getDouble("starPrice"));
			business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
			business.setRemarks(rs.getString("remarks"));
		}
		DBUtil.close(rs, ps, con);
		return business;
	}

}
